package com.shop.admin.controller;

import com.shop.admin.dto.CouponRuleDTO;
import com.shop.admin.dto.PageCouponBatchDTO;
import com.shop.admin.result.ApiResult;
import com.shop.admin.result.ApiResultList;
import com.shop.admin.service.CouponRuleService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.valid.AddGroup;
import com.shop.admin.valid.UpdateGroup;
import com.shop.admin.vo.CategoryUnderProductsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import java.util.List;

@Api(tags = {"CouponRuleController"},description = "优惠卷制作所需接口")
@RestController
@RequestMapping("api/rule")
public class CouponRuleController {

    @Resource
    private CouponRuleService couponRuleService;

    //优惠卷查询
    @PostMapping("/list")
    @ApiOperation(value = "获取所有优惠卷列表",notes = "传参：分页，数量，批次名称，卷名称，使用门槛，使用范围（1全场 2类别 3商品），优惠类型（1满送 2 满折 3满减）")
    public ApiResult list(@ApiParam("案列为：{\n" +
            "    \"page\":\"1\",\n" +
            "    \"limit\":\"10\",\n" +
            "    \"batchName\":\"c\",\n" +
            "    \"couponName\":\"ce\",\n" +
            "    \"threshold\":10,\n" +
            "    \"couponRange\":1,\n" +
            "    \"couponType\":1\n" +
            "}") @RequestBody PageCouponBatchDTO pageCouponBatchDTO){
        PageUtils page = couponRuleService.queryPageCouponRule(pageCouponBatchDTO);
        return ApiResult.ok(page);
    }

    //新增优惠卷
    @PostMapping("/save")
    @ApiOperation(value = "新增优惠卷",notes = "参数为：使用门槛,制券类型 1满送(集合） 2 满折 3满减,券使用范围 1全场 2类别 3商品(集合）,是否限制发券数量,总发放数量,\" +\n" +
            "            \"每个用户可以领取的数量,是否是互斥卷,领取开始时间,领取结束时间,使用开始时间,使用结束时间,批次名称,券名称,已发放数量,已使用数量,\" +\n" +
            "            \"制券类型所需关键字段 1满送时为商品id 2 满折为折扣 3满减为实减金额,券使用范围所需关键字段  2类别时为类别id 3商品时间为商品id")
    public ApiResult save(@Validated(AddGroup.class) @ApiParam("案列为:\"threshold\":20,\n" +
            "    \"couponType\":1,\n" +
            "    \"couponRange\":1,\n" +
            "    \"isLimitCount\":1,\n" +
            "    \"limitCount\":5,\n" +
            "    \"receiveCount\":1,\n" +
            "    \"isMutex\":1,\n" +
            "    \"receiveStarted\":\"2022-11-30 15:17:05\",\n" +
            "    \"receiveEnd\":\"2022-11-30 15:17:05\",\n" +
            "    \"useStated\":\"2022-11-30 15:17:05\",\n" +
            "    \"useEnd\":\"2022-11-30 15:17:05\",\n" +
            "    \"batchName\":\"优惠卷\",\n" +
            "    \"couponName\":\"瓜子优惠劵\",\n" +
            "    \"assignCount\":0,\n" +
            "    \"usedCount\":0,\n" +
            "    \"couponTypeNeed\": [\"1\"],\n" +
            "    \"couponRangeNeed\":1\n" +
            "}") @RequestBody CouponRuleDTO couponRuleDTO) {
        if (couponRuleService.saveCoupon(couponRuleDTO)) {
            return ApiResult.ok("新增成功!");
        }
        return ApiResult.ok("新增失败!");
    }

    //修改优惠卷
    @PostMapping("/update")
    @ApiOperation(value = "修改优惠卷",notes = "参数为：主键id,使用门槛,制券类型 1满送(集合） 2 满折 3满减,券使用范围 1全场 2类别 3商品(集合）,是否限制发券数量,总发放数量,\" +\n" +
            "            \"每个用户可以领取的数量,是否是互斥卷,领取开始时间,领取结束时间,使用开始时间,使用结束时间,批次名称,券名称,已发放数量,已使用数量,\" +\n" +
            "            \"制券类型所需关键字段 1满送时为商品id 2 满折为折扣 3满减为实减金额,券使用范围所需关键字段  2类别时为类别id 3商品时间为商品id")
    public ApiResult update(@Validated(UpdateGroup.class) @ApiParam("案列为:{\n" +
            "    \"ruleId\":4,\n" +
            "    \"threshold\":10,\n" +
            "    \"couponType\":1,\n" +
            "    \"couponRange\":1,\n" +
            "    \"isLimitCount\":1,\n" +
            "    \"limitCount\":5,\n" +
            "    \"receiveCount\":1,\n" +
            "    \"isMutex\":1,\n" +
            "    \"receiveStarted\":\"2022-11-30 15:17:05\",\n" +
            "    \"receiveEnd\":\"2022-11-30 15:17:05\",\n" +
            "    \"useStated\":\"2022-11-30 15:17:05\",\n" +
            "    \"useEnd\":\"2022-11-30 15:17:05\",\n" +
            "    \"batchName\":\"优惠卷\",\n" +
            "    \"couponName\":\"瓜子优惠劵ces1\",\n" +
            "    \"assignCount\":0,\n" +
            "    \"usedCount\":0,\n" +
            "    \"couponTypeNeed\": [\"1\",\"2\",\"3\"],\n" +
            "    \"couponRangeNeed\":1\n" +
            "}") @RequestBody CouponRuleDTO couponRuleDTO) {
        if (couponRuleService.updateCoupon(couponRuleDTO)) {
            return ApiResult.ok("修改成功!");
        }
        return ApiResult.ok("修改失败!");
    }

    @GetMapping("/products")
    @ApiOperation(value = "分类下的商品列表",notes = "")
    public ApiResultList categoryUnderProducts(){
        List<CategoryUnderProductsVo> vos = couponRuleService.categoryUnderProducts();
        return ApiResultList.ok(vos);
    }
}
